package com.zuoye.text5;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl1 extends BaseDao implements NewsDao {



    @Override
    public List<News1> getNewsList() {
        List<News1> newList=new ArrayList<News1>();
        try {
            //（3）获得Statement对象，执行SQL语句
            String sql="select * from news_detail";
            Object[] params={};
            ResultSet rs=this.executeSQL(sql, params);
            //（4）处理执行结果(ResultSet)，
            while(rs.next()){
                int id=rs.getInt("id");
                String title=rs.getString("title");
                String summary=rs.getString("summary");
                String content=rs.getString("content");
                String author=rs.getString("author");
                Timestamp time=rs.getTimestamp("createdate");

                //封装成新闻信息对象
                News1 news=new News1();
                news.setId(id);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setAuthor(author);
                news.setCreateDate(time);

                //将新闻对象放进集合中
                newList.add(news);
            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            //释放资源
            this.closeResource();
        }
        return newList;
    }


    @Override
    public boolean add(News1 news) {
        boolean flag=false;
        try {
            String sql="insert into news_detail(categoryId,title,summary,content) values(?,?,?,?)";
            Object[] params={news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent()};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("插入新闻成功！");
            }
            flag=true;
        }finally{
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public boolean delete(int id) {
        boolean flag=false;
        try {
            String sql = "delete from  news_Detail where id=?";
            Object[] params={id};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("删除新闻成功！");
            }
            flag=true;
        }  finally {
            //释放资源
            this.closeResource();
        }
        return flag;
    }

    @Override
    public boolean update(News1 news) {
        boolean flag=false;
        try {
            String sql = "update News_detail set title=? where id=?";
            Object[] params={news.getTitle(),news.getId()};
            int i=this.executeUpdate(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("修改新闻成功！");
            }
            flag=true;
        }  finally {
            //释放资源
            this.closeResource();
        }
        return flag;
    }
}
